package com.xcity.pomelo.response;

import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.db.entity.Guild;
import com.xcity.db.entity.GuildMember;
import com.xcity.db.entity.Player;
import com.xcity.game.activity.login.DailyLoginActivityService;
import com.xcity.game.activity.thirtyday.ThirtyDaySignActivityService;
import com.xcity.game.activity2.share.ShareAction;
import com.xcity.game.activity2.share.ShareActionFactory;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.common.PlayerPool;
import com.xcity.game.guild.GuildService;
import com.xcity.game.player.PlayerUtils;
import com.xcity.game.skill.SkillUtils;
import com.xcity.game.vip.VipUtils;
import com.xcity.pomelo.request.SystemSettingsRequest;
import com.xcity.util.IdUtils;

import naga.x.App;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class PlayerLoadResponse extends Response {
	
	public PlayerDetail roler;
	public short guide;
	public int serverId; // 服务器id
	
	public static class PlayerDetail {
		public int exp;
		public int curExp;
		public int nextExp;
//		@Deprecated
//		public int curOpenBuild; // 当前等级最大建筑数量
//		@Deprecated
//		public int nextOpenBuild; // 下一个最大建筑数量
//		@Deprecated
//		public int nextOpenBuildNeedLv; // 下一个最大建筑数量所需等级
		public long gold;
//		@Deprecated
//		public int health; // 风险值
		public String img;
		public String iconBorder; // 头像边框
		public int lv;
//		@Deprecated
//		public String sign;
		public long roleId;
		public String roleName;
		public byte gender;
		public long stone;
		public long totalGold;
		public int vipLv;
//		@Deprecated
//		public byte robStatus;
//		@Deprecated
//		public int robLostGold;
//		@Deprecated
//		public int robLostStone;
//		@Deprecated
//		public byte isProtect; // 资产保护
//		public int bsRemainingTime; // 点击加速道具剩余有效时间
//		@Deprecated
//		public int buildSpeed; // 加速道具速度
//		public int ipRemainingTime; // 资产保护剩余有效时间
//		public List<Build> curBuilds;
//		public List<Land> curLands;
//		public List<Financial> curFinancials;
		public int strength;
		public int bond;//债券
		public int maxStr; // 体力上限
		@JSONField(name = "isFirstLoginToday")
		public boolean isFirstLoginToday; // 是否今日第一次登录
		public String province;
//		@Deprecated
//		public int IQ, EQ, AQ,
//		@Deprecated
//		public int leadership;
		public int stage;
		public int heroStage;
		public int leftTapLikeTimes; // 剩余点赞次数
		public int charm;
//		@Deprecated
//		public int attack;
//		@Deprecated
//		public int hp;
		public byte shared; // 今日是否已分享(微博、qq说说) 1:是, 0:否
		public int sysset; // 系统设置
		public int shield; // 护盾数量
		public int boughtStrNum; // 今天已购买体力次数
		public int maxBuyStrNum; // 每天最多可购买体力次数
		public int vigour, maxVigour; // 精力
		public int qualityLvl; // 品级
		public int destinyLvl; // 天命等级
		public int starsNum; // 副本星数
		public int combatPower; // 玩家战斗力（不累加员工的战斗力）
		public int peakCoin; // 巅峰币
		public int honor; // 荣誉
		public int captureCount; // 占领建筑数量
		
		public CombatAttributeResponse attr; // 战斗属性
//		public SkillV2ToV1 skill;
		public boolean isFirstLoginToday_30day;// 30天签到 今日是否第一次登录
		public List<SkillV2Info> skills2; // 新版技能列表
		public int skp, maxSkp; // 技能点数
		public long skpCd;
		
		public byte reg; // 是否刚注册
		
		public long gid = 0;//公会ID
		public String gname = null;//公会名
		public int gscore;
		
//		public int needRecharge;//需要冲多少RMB才可以激活终身卡
	}
	
	public PlayerLoadResponse() {
		super(CODE_ERROR);
	}
	
	public PlayerLoadResponse(int serial, Player player, boolean register) {
		super(serial);
		this.roler = new PlayerDetail();
		this.roler.exp = (int) player.getExp();
		this.roler.curExp = (int) player.getExp();//player.getExpOfNowLvl();
		this.roler.nextExp = PlayerUtils.getExpCalc().getExp(player.getLevel() + 1);//(player.getNextLvlNeedExp());
//		this.roler.curOpenBuild = (player.getMaxBuildNum());
//		this.roler.nextOpenBuild = (player.getMaxBuildNumOfNextGrade());
//		this.roler.nextOpenBuildNeedLv = (player.getNextMaxBuildNumNeedLvl());
		this.roler.gold = (player.getGold());
//		this.roler.health = (player.getRisk());
//		this.roler.setId(player.getId());
		this.roler.img = (player.getIcon());
		this.roler.iconBorder = (VipUtils.getBestMatchHeadIcon(player.getVipLvl()));
		this.roler.lv = (player.getLevel());
//		this.roler.sign = (player.getSign());
		this.roler.roleId = (player.getId());
		this.roler.roleName = (player.getName());
		this.roler.gender = (player.getSex());
		this.roler.stone = (player.getStone());
		this.roler.totalGold = (player.getFixedAssets());
		this.roler.vipLv = (player.getVipLvl());
//		this.roler.robLostGold = (player.getPool().getIntValue(PlayerPool.PROPERTY_FINE_GOLD));
//		this.roler.robLostStone = (player.getPool().getIntValue(PlayerPool.PROPERTY_FINE_STONE));
//		this.roler.robStatus = ((byte) (this.roler.robLostGold > 0 || this.roler.robLostStone > 0 ? 1 : 0));
//		this.roler.isProtect = ((byte) (player.getBuffs().hasBuffOfEffect(BuffEffector.BUFF_PROTECT_ASSETS) ? 1 : 0));
//		Buff buff = player.getBuffs().getBuff(BuffEffector.BUFF_REDUCE_BUILD_CD);
//		if (buff != null) {
//			this.roler.setBsRemainingTime(buff.getRemainTime());
//			this.roler.setBuildSpeed(buff.getTemplate().getParameter(0, 1));
//		} else {
//		int cd = player.getPool().getIntValue(PlayerPool.PROPERTY_BUILD_SPEED_UP_REDUCE_CD);
//		this.roler.buildSpeed = (cd > 0 ? cd / 1000 : GameStaticConfig.speedUpReduceCD / 1000);
//		}
//		buff = player.getBuff(BuffConst.BUFF_PROTECT_ASSETS);
//		if (buff != null) {
//			this.roler.setIpRemainingTime(buff.getRemainTime());
//		}
		this.guide = (player.getPool().getShortValue(PlayerPool.PROPERTY_GUIDE));
		this.serverId = IdUtils.serverId;
//		this.roler.setCurBuilds(Build.wrap(player.getBuildManager().getBuildings()));
//		this.roler.setCurLands(Land.wrap(App.getApp().getServiceManager().get(LandService.class).getPlayerLands(player.getId())));
		this.roler.strength = player.getStrength();
		this.roler.maxStr = PlayerUtils.getMaxStrength(player);
		this.roler.isFirstLoginToday = DailyLoginActivityService.isFirstLoginToday(player);
		this.roler.province = player.getProvince();
//		PlayerLevelTemplate playerTemplate = PlayerUtils.getTemplate(player);
//		this.roler.IQ = playerTemplate.getIQ();
//		this.roler.EQ = playerTemplate.getEQ();
//		this.roler.AQ = playerTemplate.getAQ();
//		this.roler.leadership = PlayerUtils.getLeadership(player);
		this.roler.stage = player.getPveStages().getStages().get(player.getPveStages().getCurrentIndex((byte) 0)).getId();
		this.roler.heroStage = player.getPveStages().getStages().get(player.getPveStages().getCurrentIndex((byte) 1)).getId();
		this.roler.leftTapLikeTimes = PlayerUtils.getLeftTapLikeTimes(player);
		this.roler.charm = PlayerUtils.getCharm(player);
//		this.roler.attack = (int) player.getAttackPower();//PlayerUtils.getAttack(player);
//		this.roler.hp = player.getHp();//PlayerUtils.getHP(player);
		ShareAction sa = ShareActionFactory.create(player.getAccount().getChannel());
		this.roler.shared = (byte) (sa != null && sa.isShared(player) ? 1 : 0);
		this.roler.sysset = player.getPool().getInt(PlayerPool.PROPERTY_SYSTEM_SETTINGS, SystemSettingsRequest.DEFAULT_VALUE);
		this.roler.shield = player.getShield();
		this.roler.boughtStrNum = PlayerUtils.getBoughtStrengthTimes(player);
		this.roler.maxBuyStrNum = PlayerUtils.getBuyStrengthMaxTimes(player);
		this.roler.vigour = player.getVigour();
		this.roler.maxVigour = PlayerUtils.getMaxVigour(player);
		this.roler.qualityLvl = player.getQualityLvl();
		this.roler.destinyLvl = player.getDestinyLvl();
		this.roler.starsNum = player.getStageStars();
		this.roler.combatPower = player.getCombatPower();//CombatUtils.calcCombatPowerWithEmployees(player);
		this.roler.peakCoin = player.getPeakCoin();
		this.roler.honor = player.getHonor();
		this.roler.attr = new CombatAttributeResponse(player);
//		this.roler.skill = new SkillV2ToV1(player.getSkillManager().getActive());//player.getSkillManager() != null ? player.getSkillManager().getActivatedSkill() : null;
		this.roler.bond = player.getBond();
		this.roler.isFirstLoginToday_30day = ThirtyDaySignActivityService.isFirstLoginToday(player);
		this.roler.captureCount = player.getConquests().size();
		
		this.roler.skills2 = SkillUtils.toResponse(player);
		this.roler.skp = player.getSkillPoints();
		this.roler.maxSkp = GameStaticConfig.maxSkillPoints;
		this.roler.skpCd = player.getSkillPointsCD();
		
		this.roler.reg = (byte) (register ? 1 : 0);
		
		GuildMember member = App.getApp().getServiceManager().get(GuildService.class).getGuildMember(player.getId());
		if(member != null){
			this.roler.gid = member.getGuildid();
			if(this.roler.gid > 0){
				Guild guild = App.getApp().getServiceManager().get(GuildService.class).findGuildById(this.roler.gid);
				if(guild != null){
					this.roler.gname = guild.getName();
				}
			}
		}
		this.roler.gscore = player.getGuildScore();
//		int needRecharge = -1;
//		if(player.getPool().getInt(PlayerPool.PROPERTY_LAST_RECHARGE_YEAR, 0) <= 0){
//			needRecharge = MoonCardActivityService.YEARCARD_ACTIVE_RMB - player.getPool().getInt(PlayerPool.PROPERTY_YEAR_CARD_TOTAL_RECHARGE, 0);
//			if(needRecharge <= 0){
//				needRecharge = 0;
//			}
//		}
//		this.roler.needRecharge = needRecharge;
	}
	
//	public static class Build {
//		public int buildId;
//		public int health;
//		public int id;
//		public String img;
//		public int income;
//		public int level;
//		public int maxIncome;
//		public int money;
//		public String name;
//		public int price;
//		public int remainingTime;
//		public int cdTime;
//		public byte status; // 0-正在建造/升级; 1-正常
//		static List<Build> wrap(Player player, List<com.xcity.db.entity.Build> srcs) {
//			if (srcs == null || srcs.isEmpty()) {
//				return Collections.emptyList();
//			}
//			List<Build> result = new ArrayList<Build>(srcs.size());
//			for (int i = 0; i < srcs.size(); i++) {
//				com.xcity.db.entity.Build src = srcs.get(i);
//				Build b = new Build();
//				b.buildId = src.getId();
//				BuildLevelUpTemplate tempLv = src.getTemplate().getLevelUpTemplate(src.getLevel());
//				b.health = tempLv.getRisk();
//				b.id = src.getId();
//				b.img = src.getTemplate().getImage(src.getLevel());
//				b.income = (int) (/*tempLv.getIncome()*/src.getIncomePerMinute() * src.getFactor());
//				b.level = src.getLevel();
//				b.maxIncome = /*tempLv.getMaxIncome()*/src.getMaxIncome();
//				b.money = src.settle();
//				b.name = src.getTemplate().getName(src.getLevel());
//				b.price = tempLv.getPrice();
//				b.remainingTime = src.getLeftCD();
//				b.cdTime = tempLv.getCd() * 1000;
//				b.status = (byte) (b.cdTime > 0 ? 0 : 1);
//				result.add(b);
//			}
//			return result;
//		}
//	}
	
//	public static class Land {
//		public int buildId;
//		public String name;
//		public int getBuildId() {
//			return buildId;
//		}
//		public void setBuildId(int buildId) {
//			this.buildId = buildId;
//		}
//		public String getName() {
//			return name;
//		}
//		public void setName(String name) {
//			this.name = name;
//		}
//		static List<Land> wrap(List<com.xcity.db.entity.Land> srcs) {
//			if (srcs == null || srcs.isEmpty()) {
//				return Collections.emptyList();
//			}
//			List<Land> result = new ArrayList<Land>(srcs.size());
//			for (int i = 0; i < srcs.size(); i++) {
//				com.xcity.db.entity.Land src = srcs.get(i);
//				Land l = new Land();
//				l.buildId = src.getId();
//				l.name = src.getTemplate().getName();
//				result.add(l);
//			}
//			return result;
//		}
//	}

}
